//
//  UIColor+Extensions.swift
//  SwiftDemo
//
//  Created by Chris on 2017/6/13.
//  Copyright © 2017年 chris. All rights reserved.
//

import UIKit

extension UIColor {
    
    /// 使用 R / G / B 数值创建颜色
    ///
    /// - Parameters:
    ///   - red: red
    ///   - green: green
    ///   - blue: blue
    ///   - alpha: 透明度
    /// - Returns: 颜色
    class func cj_init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat = 1.0) -> UIColor {
        return UIColor.init(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: alpha)
    }
    
    /// 使用 16 进制数字创建颜色，例如 0xFF0000 创建红色
    ///
    /// - Parameter hex: 16 进制无符号32位整数
    /// - Returns: 颜色
    class func cj_colorWithHex(hex: UInt32) -> UIColor {
        
        let r = UInt8((hex & 0xff0000) >> 16)
        let g = UInt8((hex & 0x00ff00) >> 8)
        let b = UInt8(hex & 0x0000ff)
        
        return self.cj_init(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: 1.0)
    }
    
    /// 随机颜色
    ///
    /// - Returns: 颜色
    class func cj_randomColor() -> UIColor {
        return self.cj_init(red: CGFloat(arc4random_uniform(256)), green: CGFloat(arc4random_uniform(256)), blue: CGFloat(arc4random_uniform(256)), alpha: 1.0)
    }
    
}
